<template>
  <Container class="voucher-product">
    <div slot="header" class="container-header title title-center">满额返劵</div>
    <ActiveInfo :is-large="isLarge" />
    <div v-show="getGiftJoinType!=='WHOLE'" class="container-header mt">
      <SearchSale ref="search" :is-large="isLarge" @onSearchData="onSearchData" />
    </div>
    <div v-show="getGiftJoinType!=='WHOLE'" class="bg-light mt">
      <Tabs v-model="tab" class="mt" @on-click="onChoiceTab">
        <template slot="extra">
          <template v-if="tab === 'tab1'">
            <Button class="item" type="primary" icon="md-add" @click="onJoinAdd">批量加入</Button>
          </template>
          <template v-if="tab === 'tab2'">
            <Button class="item" type="primary" icon="md-add" @click="onJoinDelete">批量删除</Button>
          </template>
        </template>
        <TabPane label="选择主商品" name="tab1">
          <choiceCommidy ref="choiceCommidy" />
        </TabPane>
        <TabPane :label="label1" name="tab2">
          <mainCommidy
            ref="mainCommidy"
            :total-item="totalItem"
          />
        </TabPane>
      </Tabs>
    </div>
    <div
      v-show="getGiftJoinType=='WHOLE'"
      class="set-h2 mt"
    >您已选择了全场参加，店铺所有商品都会参加！</div>
    <div slot="footer" class="container-footer bg-white">
      <Button size="large" @click="onJoinCancel">返回</Button>
      <Button type="primary" size="large" class="ml" @click="onJoinList">关闭</Button>
      <Button type="primary" size="large" class="ml" :loading="loading" @click="onPrevertion">上架</Button>
    </div>
    <Loader :status="status" @refresh="getGiftData" />
  </Container>
</template>
<script>
import choiceCommidy from './list/ChoiceCommidyItem'
import SearchSale from '@/views/sales/common/SearchSale'
import mainCommidy from './list/MainCommidyItem'
import ActiveInfo from './edit/ActiveInfo'
import { columns1, columns2, data1, data2 } from './edit/FullReturnListMethods'
import { getPuduct } from '@/views/sales/common/TableSaleMethods'
import { getValue, getValue1, formatDate } from './edit/SaleListMethods'
import { mapGetters } from 'vuex'
export default {
  name: 'fullreturnlist',
  components: {
    choiceCommidy,
    mainCommidy,
    ActiveInfo,
    SearchSale
  },
  data () {
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      tab: 'tab1',
      loading: false,
      getParam: '3008263813595770378',
      columns1,
      columns2,
      data1,
      data2,
      label1: (h) => {
        return h('div', [
          h('span', '已选主商品'),
          h('Badge', {
            props: {
              count: this.selectProductNum
            }
          })
        ])
      },
      isLarge: true,
      totalItem: 1,
      status: 'pass',
      isShelf: 'ON'
    }
  },
  mounted () {
    this.getGiftData()
    this.$refs.choiceCommidy.getProductData()
    this.$store.dispatch('getOtherSkuCount', { id: this.id, self: this }).then(res => {
      this.status = 'pass'
    })
    this.id && this.$store.dispatch('getShelfStatus', this.id).then(data => {
      this.isShelf = data
    })
    // if (this.id && !this.$store.state.fullReturnSale.GetsaleData.length) this.getData()
  },
  methods: {
    onPrevertion (type) {
      this.$jsonPost(`/activity/full_gift_coupon/shelf/${this.id}`).then((data) => {
        this.$Message.success('活动上架成功')
        this.$router.push({ name: 'sales/list' })
      })
    },
    onJoinList () {
      this.$router.push({ name: 'sales/list' })
    },
    getDetailData () {
      this.$store.dispatch('getGiftDetailData', this.id)
    },
    getGiftData () {
      let self = this
      self.status = 'loading'
      this.$store.dispatch('getGiftCouponList').then(res => {
        res.forEach(item => {
          self.$store.state.fullReturnSale.FullreturnForm.giftCoupon[item.id] = item.couponName
        })
        self.getDetailData()
      })
    },
    onChoiceTab (e) {
      this.$store.commit('setSetOtherSearchEmpty')
      this.$refs.search.searchForm = {
        brandId: null,
        categoryId: null,
        keyword: '',
        code: '',
        skuCode: '',
        tireFigure: null,
        tireRim: null,
        specs: ''

      }
      if (e == 'tab1') {
        this.$refs.choiceCommidy.getProductData()
        this.$refs.search.isLargeShow = 'tab1'
      } else if (e == 'tab2') {
        this.$refs.search.isLargeShow = 'tab2'
        this.$refs.mainCommidy.getSkuPage({ size: window.$pageSize, current: 1, self: this })
      }
    },
    onSearchData (param) {
      this.$store.commit('onSetOtherSearch', param)
      if (this.tab == 'tab1') {
        this.$refs.choiceCommidy.setPage()
        this.$refs.choiceCommidy.getProductData(param)
      } else if (this.tab == 'tab2') {
        this.$refs.mainCommidy.setPage()
        // this.$store.dispatch('getOtherSkuPage', param).then(res => {
        // })
      }
    },
    onJoinCancel () {
      this.$router.back()
    },
    jumpPrevPage () {
      this.$router.back()
    },
    onJoinAdd () {
      this.$refs.choiceCommidy.getSaleData('checked')
    },
    onJoinDelete () {
      this.$refs.mainCommidy.deleteSaleData('checked')
    },
    onAddPost () {
      let self = this
      if (!self.getSaleNum) {
        self.$Message.error('请先选择商品')
        return false
      }
      self.loading = true
      let param = {
        couponInfoDTOList: getValue1(
          this.$store.state.fullReturnSale.FullreturnForm.activityGiftCouponList,
          {
            index1: 'denomination',
            index2: 'useCondition',
            index3: 'validityDays',
            index4: 'custom'
          }
        ), //优惠劵
        fullGiftCouponDetailDTO: {
          activityFullGiftCouponDTO: {
            giftCouponRuleList: getValue(this.$store.state.fullReturnSale.FullreturnForm.activityGiftCouponList, { index: 'value', index1: 'isParcel' }),
            isFirstOrderGift: this.isGender, //是否仅首次首单返券
            limitCompleteDatetime: this.limitCompleteDatetime, //用户在之前完成订单
            limitReturnDate: this.$store.state.fullReturnSale.FullreturnForm.time // 天内，未申请退款
          },
          activityInfoDTO: {
            activityName: this.$store.state.fullReturnSale.FullreturnForm.name, //活动名称
            productSkuList: this.productSkuList,
            startDate: this.timeList[0].startDatetime,
            endDate: this.timeList[0].endDatetime,
            shelfStatusEnum: 'ON',
            activityDescription: this.$store.state.fullReturnSale.FullreturnForm.remarks, //活动备注
            joinTypeEnum: 'PART'
          }
        }
      }
      this.$jsonPost('/activity/full_gift_coupon/save', param).then(data => {
        self.$Message.success('新增活动成功')
        self.$store.commit('setFullreturnSaleEmpty')
        self.$router.push({ name: 'sales/list' })
        self.loading = false
      }
      ).catch(data => {
        self.loading = false
      })
    }
  },
  computed: {
    ...mapGetters([
      'selectProductNum',
      'getGiftJoinType'
    ]),
    getSaleNum () {
      return this.$store.getters.getfullReturnSaleNum
    },
    timeList () {
      return [
        {
          startDatetime: formatDate(
            this.$store.state.fullReturnSale.FullreturnForm.date[0],
            true
          ),
          endDatetime: formatDate(
            this.$store.state.fullReturnSale.FullreturnForm.date[1],
            true
          )
        }
      ]
    },
    limitCompleteDatetime () {
      return formatDate(
        this.$store.state.fullReturnSale.FullreturnForm.endTime,
        true
      )
    },
    productSkuList () {
      return getPuduct(this.$store.state.fullReturnSale.GetsaleData, {
        child: 'productSkuList',
        getParam: ['productId', 'id', 'parentcode', 'code']
      })
    },
    isGender () {
      if (this.$store.state.fullReturnSale.FullreturnForm.gender == 1) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

